package com.yzlshdg;

import com.yzlshdg.entity.BaGua;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.stage.Stage;



import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

//这种写法，不加这个注解。就加载不了配置类  配置类所在的包
@ComponentScan(basePackages = "com.yzlshdg.config")
public class MainApp extends Application {



    private ConfigurableApplicationContext context;




    @Override
    public void init() throws Exception {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(MainApp.class);
        context = builder.run(getParameters().getRaw().toArray(new String[0]));
    }

    @Override
    public void start(Stage stage) throws Exception {

/*    //加载fxml文件
       AnchorPane root = FXMLLoader.load(getClass().getResource("/fxml/app.fxml"));*/


        //创建可观察的列表，用来存数据
        ObservableList<BaGua> data = FXCollections.observableArrayList();

        //创建表
        TableView<BaGua> table = new TableView<>();
//        table.setPrefWidth(969.0);
//        table.prefHeight(620.0);
        //创建列
        TableColumn eight = new TableColumn<>("八卦");
        eight.setPrefWidth(95.20001524686813);
        TableColumn shiW = new TableColumn<>("事物");
        shiW.setPrefWidth(95.20001524686813);
        TableColumn fuH = new TableColumn<>("符号");
        fuH.setPrefWidth(95.20001524686813);
        TableColumn fangX = new TableColumn<>("方向");
        fangX.setPrefWidth(95.20001524686813);
        TableColumn jieQ = new TableColumn<>("节气");
        jieQ.setPrefWidth(95.20001524686813);
        TableColumn yueF = new TableColumn<>("月份");
        yueF.setPrefWidth(95.20001524686813);
        TableColumn yueX = new TableColumn<>("月相");
        yueX.setPrefWidth(95.20001524686813);
        TableColumn zhouY = new TableColumn<>("周易");
        zhouY.setPrefWidth(319.2000732421875);


        //把数据放入对应的列
        eight.setCellValueFactory(new PropertyValueFactory<>("eight"));
        shiW.setCellValueFactory(new PropertyValueFactory<>("shiW"));
        fuH.setCellValueFactory(new PropertyValueFactory<>("fuH"));
        fangX.setCellValueFactory(new PropertyValueFactory<>("fangX"));
        jieQ.setCellValueFactory(new PropertyValueFactory<>("jieQ"));
        yueF.setCellValueFactory(new PropertyValueFactory<>("yueF"));
        yueX.setCellValueFactory(new PropertyValueFactory<>("yueX"));
        zhouY.setCellValueFactory(new PropertyValueFactory<>("zhouY"));


                           //从Spring容器中获取JdbcTemplate实例，用于执行数据库操作。执行数据库查询操作，第一个参数是SQL查询语句
        List<BaGua> list = context.getBean(JdbcTemplate.class).query("select * from 先天八卦",
                //第二个参数是一个lambda表达式，用于处理查询结果
            // 这是一个lambda表达式，用于处理查询结果。它接收两个参数：ResultSet rs表示查询结果集，int rowNum表示当前行号 从0开始
                (rs, rowNum) -> {
              //通过ResultSet对象的getString()方法获取名为"八卦"的列的值，并赋给eightVal变量
                    String eightVal = rs.getString("八卦");
                    String shiWVal = rs.getString("事物");
                    String fuHVal = rs.getString("符号");
                    String fangXVal = rs.getString("方向");
                    String jieQVal= rs.getString("节气");
                    String yueFVal = rs.getString("月份");
                    String yueXVal = rs.getString("月相");
                    String zhouYVal = rs.getString("周易");
                    //根据从查询结果中提取的值，创建一个新的BaGua对象，并返回该对象。这个类是数据模型类
                    return new BaGua(eightVal,shiWVal,fuHVal,fangXVal,jieQVal,yueFVal,yueXVal,zhouYVal);
                });

        //foreach循环遍历这个集合
        for(BaGua baGua:list) {
           data.add(new BaGua(baGua.getEight(),baGua.getShiW(),baGua.getFuH(),baGua.getFangX()
           ,baGua.getJieQ(),baGua.getYueF(),baGua.getYueX(),baGua.getZhouY()));
        }


        //把列放到表格中
        table.getColumns().addAll(eight,shiW,fuH,fangX,jieQ,yueF,yueX,zhouY);
        //把数据放入表中
        table.setItems(data);
/*

        Button button = new Button("按钮");
        button.setPrefWidth(100);
        button.setPrefHeight(50);


        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent actionEvent) {
                //创建可观察的列表，用来存数据
                ObservableList<BaGua> data = FXCollections.observableArrayList();

                //创建表
                TableView<BaGua> table = new TableView<>();
     //        table.setPrefWidth(969.0);
     //        table.prefHeight(620.0);
                //创建列
                TableColumn eight = new TableColumn<>("八卦");
                eight.setPrefWidth(95.20001524686813);
                TableColumn shiW = new TableColumn<>("事物");
                shiW.setPrefWidth(95.20001524686813);
                TableColumn fuH = new TableColumn<>("符号");
                fuH.setPrefWidth(95.20001524686813);
                TableColumn fangX = new TableColumn<>("方向");
                fangX.setPrefWidth(95.20001524686813);
                TableColumn jieQ = new TableColumn<>("节气");
                jieQ.setPrefWidth(95.20001524686813);
                TableColumn yueF = new TableColumn<>("月份");
                yueF.setPrefWidth(95.20001524686813);
                TableColumn yueX = new TableColumn<>("月相");
                yueX.setPrefWidth(95.20001524686813);
                TableColumn zhouY = new TableColumn<>("周易");
                zhouY.setPrefWidth(319.2000732421875);

                //对应数据导入的列
                eight.setCellValueFactory(new PropertyValueFactory<>("eight"));
                shiW.setCellValueFactory(new PropertyValueFactory<>("shiW"));
                fuH.setCellValueFactory(new PropertyValueFactory<>("fuH"));
                fangX.setCellValueFactory(new PropertyValueFactory<>("fangX"));
                jieQ.setCellValueFactory(new PropertyValueFactory<>("jieQ"));
                yueF.setCellValueFactory(new PropertyValueFactory<>("yueF"));
                yueX.setCellValueFactory(new PropertyValueFactory<>("yueX"));
                zhouY.setCellValueFactory(new PropertyValueFactory<>("zhouY"));

                //把列放到表格中
                table.getColumns().addAll(eight, shiW, fuH, fangX, jieQ, yueF, yueX, zhouY);



                stage.setScene(new Scene(table));

            }
        });

*/


        stage.setWidth(969);
        stage.setHeight(720);
        stage.getIcons().add(new Image("/image/xx.jpg"));
        stage.setScene(new Scene(table));
        stage.setTitle("JavaFX程序");
        stage.show();
    }
}
